import { Camera, Canvas, error, find,instantiate,Node, Prefab, SpriteAtlas, TextAsset, UIComponent } from "cc";
import { BaseClass } from "./BaseClass";
import { ExcelMgr } from "./ExcelMgr";
import { ResManager } from "./ResManager";

export enum UIType {
    Default = 0,    //直接加载到Canvas下面的
    Full = 1,       //全屏界面
    UnFull = 2,     //弹框界面
}

export class UIMgr extends BaseClass{
    private uiMap: any = {};
    private uiCanvas: Canvas = null;
    private uiCamera: Camera = null;
    // 全屏界面
    private rootFull: Node = null;
    // 弹框界面
    private rootPop: Node = null;

    private static _inst: UIMgr;
    public static get inst(): UIMgr {
        if (this._inst == null) {
            this._inst = new UIMgr();
        }
        return this._inst;
    }

    constructor(){
        super();
        this.Init();
    }

    public Init(): void {
        var canvasNode = find("Canvas");
        if(!canvasNode) {
            error("[UIMgr]:can not find UI main Canvas !");
            return;
        }

        this.uiMap = {};
        this.uiCanvas = canvasNode.getComponent(Canvas);
        this.uiCamera = canvasNode.getChildByName("Camera").getComponent(Camera);

        this.rootFull = this.uiCanvas.node.getChildByName('rootFull');
        this.rootPop = this.uiCanvas.node.getChildByName('rootPop');
    }

    /**
     * 
     * @param type  type = 0是直接加载到Canvas下面的, type = 1 是rootFull全屏界面， type = 2是弹框界面
     * @returns 
     */
    public getParent(type: number = UIType.Full): Node|null{
        var root = this.uiCanvas.node;
        if(type === UIType.Default){
            return root;
        }else{
            if(type === UIType.Full){
                root = this.rootFull;
            }else if(type === UIType.UnFull){
                root = this.rootPop;
            }
            root.destroyAllChildren();
            return root;
        }   
    }

    public ShowUIPrefab(uiPrefab: Prefab, parent: Node = null) {
        var uiView: Node = instantiate(uiPrefab) as Node;
        parent = (!parent)? this.uiCanvas.node : parent;
        parent.addChild(uiView);

        //往根节点上挂下UI视图脚本;
        var com = uiPrefab.data.name + "_Ctr";
        var uiCtrl = uiView.getComponent(com);
        if(!uiCtrl){
            uiCtrl = uiView.addComponent(com);
        }
        this.uiMap[uiPrefab.data.name] = uiView;

        return uiCtrl;
    }

    /**
     * 
     * @param bundleName 
     * @param assetName 
     * @param type  type = 0是直接加载到Canvas下面的, type = 1 是rootFull全屏界面， type = 2是弹框界面
     * @param any 
     */
    public ShowUIView(bundleName: string, assetName: string, type: number = UIType.UnFull) {
        let uiPrefab = ResManager.inst.getAsset(bundleName, assetName, Prefab);
        if (!uiPrefab) {
            console.log("cannot find ui Prefab: ", assetName);
            return null!;
        }
        let parent = this.getParent(type);
        return this.ShowUIPrefab(uiPrefab as Prefab, parent);
    }

    public DestroyUIView(viewName: string): void {
        if(!this.uiMap[viewName]) {
            return;
        }

        this.uiMap[viewName].destroy();
        this.uiMap[viewName] = null;
    }

    // 对话框等节点池用这个删除一个UI
    public RemoveUIViewFromParent(viewName: string): Node {
        if(!this.uiMap[viewName]) {
            return null;
        }

        this.uiMap[viewName].removeFromParent();
        var item = this.uiMap[viewName];
        this.uiMap[viewName] = null;

        return item;
    }

    /**
   * 获取图集资源
   * @param path  路径
   * @param abName ab包名
   * @returns 
   */
    public getAtals(path: string, abName: string): SpriteAtlas {
        let sprAtals = ResManager.inst.getAsset(abName,path,SpriteAtlas) as SpriteAtlas;
        if (!sprAtals) {
            console.error(`No atlas named ${path} was found`);
        }
        return sprAtals;
    }

    /**
     * 获取excle表格数据
     * @param path 
     * @param excleName 
     * @param abName 
     * @returns 
     */
    public  getExcleData(path: string,excleName:string, abName: string){
        var textAsset = ResManager.inst.getAsset(abName,path,TextAsset) as TextAsset;
        if(textAsset){
            ExcelMgr.inst.AddTable(excleName, (textAsset as TextAsset).text);
            return true;
        }else{
            console.error('can not find ' + path);
            return false;
        }
    }
}